<?php
/**
 * Created by PhpStorm.
 * User: Dick
 * Date: 2016/3/28
 * Time: 1:00
 */

namespace Model;


use Method\JSON;

class Picture
{
    public $user;
    public $photo;//图片地址
    public $datetime;

    public function upload($userid, $file, $datetime)
    {
        global $pdo;

        list(, $file) = explode(',', $file);
        $file = str_replace(' ', '+', $file);
        $type = '.jpg';
        $new_name = md5(uniqid());//基于以微秒计的当前时间，生成唯一ID
        $target = 'Lib/Resource/Image/' . $new_name . $type;
        if (!file_put_contents($target, base64_decode($file))) {
            return;
        }


        $inTransaction = $pdo->inTransaction();//判断事务开启
        if (!$inTransaction) {
            $pdo->beginTransaction();//开启事务
        }
        $statament = $pdo->prepare("INSERT INTO image SET `user`=?,photo=?,datetime=?");
        $statament->bindValue(1, $userid);
        $statament->bindValue(2, $target);
        $statament->bindValue(3, $datetime);
        $statament->execute();
        $this->id = $pdo->lastInsertId();
        if (!$inTransaction) {
            $pdo->commit();
        }
        $result = array(
            'id' => $this->id,
            'path' => $target
        );
        JSON::json($result);
    }

    public static function delpic($picid)
    {
        global $pdo;
        $inTransaction = $pdo->inTransaction();//判断事务开启
        if (!$inTransaction) {
            $pdo->beginTransaction();//开启事务
        }
        $statament = $pdo->prepare("SELECT photo FROM image WHERE id=?");
        $statament->bindValue(1, $picid);
        $statament->setFetchMode(\PDO::FETCH_ASSOC);
        $statament->execute();
        $result = $statament->fetch();

        $statament = $pdo->prepare("DELETE FROM image WHERE id=?");
        $statament->bindValue(1, $picid);
        $statament->execute();
        if (!$inTransaction) {
            $pdo->commit();
        }
        unlink($result['photo']);
    }

    /**
     * save avatar to datebase
     * */
    public function uploadtx($userid, $file)
    {
        global $pdo;

        list(, $file) = explode(',', $file);
        $file = str_replace(' ', '+', $file);
        $type = '.jpg';
        $new_name = md5(uniqid());//基于以微秒计的当前时间，生成唯一ID
        $target = 'Lib/Resource/Image/' . $new_name . $type;
        if (!file_put_contents($target, base64_decode($file))) {
            return;
        }

        $inTransaction = $pdo->inTransaction();//判断事务开启
        if (!$inTransaction) {
            $pdo->beginTransaction();//开启事务
        }
        $statament = $pdo->prepare("UPDATE user SET avatar=:target WHERE id=:userid");
        $statament->bindValue(':target', $target);
        $statament->bindValue(':userid', $userid);
        $statament->execute();
        $_SESSION['user']->avatar = $target;
        if (!$inTransaction) {
            $pdo->commit();
        }
    }

    public function uploadbg($userid, $file)
    {
        global $pdo;

        list(, $file) = explode(',', $file);
        $file = str_replace(' ', '+', $file);
        $type = '.jpg';
        $new_name = md5(uniqid());//基于以微秒计的当前时间，生成唯一ID
        $target = 'Lib/Resource/Image/' . $new_name . $type;
        if (!file_put_contents($target, base64_decode($file))) {
            return;
        }

        $inTransaction = $pdo->inTransaction();//判断事务开启
        if (!$inTransaction) {
            $pdo->beginTransaction();//开启事务
        }
        $statament = $pdo->prepare("UPDATE user SET `bg`=:target WHERE id=:userid");
        $statament->bindValue(':target', $target);
        $statament->bindValue(':userid', $userid);
        $statament->execute();
        if (!$inTransaction) {
            $pdo->commit();
        }

        $lastbg = $_SESSION['user']->bg;
        unlink($lastbg);
        $_SESSION['user']->bg = $target;
    }
}